package com.bwie;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/6/1 19:04
 */


// 9.使用synchronized关键字保护共享资源（10分）
public class BookList1 {

    private List<Book> bookList = new ArrayList<>();

    public synchronized Book addBook(Book book) {
        bookList.add(book);
        return book;
    }

    public synchronized Book getBook(int index) {
        return bookList.get(index);
    }

     public synchronized Book removeBook(int index) {
        return bookList.remove(index);
    }

    public void printAll () {
        for (Book book : bookList) {
            System.out.println(book);
        }
    }

    public static void main(String[] args) {
        BookList1 bookList = new BookList1();

        // 添加图书
        bookList.addBook(new Book("1", "title1", "author1", "publisher1"));
        bookList.addBook(new Book("2", "title2", "author2", "publisher2"));
        bookList.addBook(new Book("3", "title3", "author3", "publisher3"));

        System.out.println(bookList.getBook(1));
        System.out.println("=============================");

        // 删除 索引为1的图书
        bookList.removeBook(1);

        // 打印所有图书信息
        bookList.printAll();
    }
}
